package br.senai.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import br.senai.dao.ProfessorDAO;
import br.senai.entity.Professor;

/**
 * Servlet implementation class ProfessorServlet
 */
@WebServlet("/ProfessorServlet")
public class ProfessorServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		Long idprofessor = Long.parseLong(request.getParameter("idprofessor"));
		String nome = request.getParameter("Nome");
		String cpf = request.getParameter("CPF");
		String telefone = request.getParameter("Telefone");
		String endereco = request.getParameter("Endereco");
		
		Professor professor = new Professor();
		ProfessorDAO dao = new ProfessorDAO();
		professor.setNome(nome);
		professor.setCpf(cpf);
		professor.setTelefone(telefone);
		professor.setEndereco(endereco);
		
		if(idprofessor.equals(0) || idprofessor.equals(null) ){
			dao.adicionarProfessor(professor);	
		} else {
			professor.setId_professor(idprofessor);
			dao.editarProfessor(professor);
		}
		
		RequestDispatcher rd = request.getRequestDispatcher("ListaProfessor");
		rd.forward(request, response);

	}

}
